//
// Created by 11510 on 2024/6/21.
//
#include <iostream>
#include <memory>

using std::cout;
using std::endl;
using std::shared_ptr;
using std::unique_ptr;

class Point {
public:
    Point(int x, int y)
            : _ix(x), _iy(y) {}

    void print() const {
        cout << "(" << _ix
             << "," << _iy
             << ")" << endl;
    }

private:
    int _ix;
    int _iy;
};

void test0() {
    //需要人为避免
    Point *p1 = new Point(1, 2);
    unique_ptr<Point> up(p1);
    unique_ptr<Point> up2(p1);
    up->print();
    up2->print();
}


void test1() {
    unique_ptr<Point> up(new Point(1, 2));
    unique_ptr<Point> up2(new Point(1, 2));
    up->print();
    up2->print();
}

void test2() {
    //需要人为避免
    Point *p1 = new Point(1, 2);
    shared_ptr<Point> sp(p1);
    shared_ptr<Point> sp2(p1);
    sp->print();
    sp2->print();
    cout << sp.use_count() << endl;
    cout << sp2.use_count() << endl;
}

void test3(){
    Point * p1 = new Point(1,2);
    shared_ptr<Point> sp(p1);
    shared_ptr<Point> sp2(sp);
    sp->print();
    sp2->print();
    cout << sp.use_count() << endl;
    cout << sp2.use_count() << endl;
}

int main(void) {
    test3();
    return 0;
}